﻿#region License statement
// NJamb, a specification and delta-specification DSL
// Copyright (c) 2010-2011, Mark Knell
// Published under the MIT License; all other rights reserved
#endregion

#region using...
using System;
using System.Reflection;
using NJamb.NUnit;
using NJamb.Parser.SubjectBuilders;
using NJamb.Specifications.Void;
using NUnit.Framework;
using Stile.Patterns.SelfDescribingPredicates;
using Stile.Readability;
using Stile.Testing.SampleTypes;
#endregion

namespace NJamb.Tests.Parser.SpecificationBuilders
{
    [TestFixture]
    public class SpecificationBuilderFixture : EvaluatingSampleFixture
    {
        //[Test]
        //public void DoesNotThrow_Delta()
        //{
        //    // arrange
        //    _sample.Thrower = () =>
        //                      {
        //                          throw new AmbiguousMatchException();
        //                      };

        //    IDeltaSpecification deltaSpecification =
        //        Specify.For(() => _sample).That(x => x.Throw()).Throws<AmbiguousMatchException>() //
        //            .AndLater.DoesNotThrow();

        //    var preconditionEvaluation = deltaSpecification.EvaluatePrecondition();

        //    Assert.That(preconditionEvaluation.Evaluation.Outcome, Is.True, "Precondition");

        //    // act
        //    IEvaluation after = preconditionEvaluation.GetPostcondition().Evaluate();

        //    // assert
        //    AssertEvaluationFailed(after,
        //        "expected expected _sample.Throw() would not throw",
        //        " but threw AmbiguousMatchException.");
        //}

        [Test]
        public void Throws_NotThrown()
        {
            _sample.Thrower = () =>
                              {
                                  throw new AmbiguousMatchException();
                              };
            // arrange
            IVoidSpecification<Sample> voidSpecification =
                Specify.For(() => _sample).That(x => x.Throw()).Throws<AmbiguousMatchException>();

            // act
            IEvaluation evaluation = voidSpecification.Evaluate();

            // assert
            Assert.That(evaluation.Success, Is.True);
            _sample.Thrower = Empty.Action;

            AssertEvaluationFailed(voidSpecification.Evaluate(),
                "expected _sample.Throw() would throw AmbiguousMatchException",
                " but no exception was thrown.");
        }

        [Test]
        public void Throws_ThrewDifferentException()
        {
            _sample.Thrower = () =>
                              {
                                  throw new ArgumentOutOfRangeException();
                              };
            // arrange
            IVoidSpecification<Sample> voidSpecification =
                Specify.For(() => _sample).That(x => x.Throw()).Throws<AmbiguousMatchException>();

            // act
            IEvaluation evaluation = voidSpecification.Evaluate();

            // assert
            AssertEvaluationFailed(evaluation,
                "expected _sample.Throw() would throw AmbiguousMatchException",
                " but threw ArgumentOutOfRangeException.");
        }
    }
}
